Option Strict Off
Option Explicit On
Namespace BMLERP
	Friend Class frmStaff
		Inherits System.Windows.Forms.Form
		Dim AnPhim As Byte
		Sub mhnvien()
			Lbl_UserCode.Text = ""
			txt_Name.Text = ""
			Lbl_UserName.Text = ""
			Lbl_GroupName.Text = ""
		End Sub

		Private Sub cboName_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboName.ClickEvent
			Dim UserGroup As Object
			Dim RecUsers As ADODB.Recordset
			If eventArgs.area = 2 Then
				ISEDITING = True
				RecUsers = DBconn.Execute("Select * from Users where usercode='" & CboName.BoundText & "'")
				'Adodc1.Recordset.Requery

				With RecUsers
					If Not .EOF Then
						txt_Name.Text = .Fields("Name").Value
						Lbl_UserCode.Text = .Fields("usercode").Value
						Lbl_UserName.Text = IIf(IsDBNull(.Fields("UserName").Value), "", .Fields("UserName").Value)
						UserGroup = .Fields("UserGroup").Value
					End If

					.Close()
				End With

				If Not IsDBNull(UserGroup) And UserGroup <> "" Then
					With Adodc2.Recordset
						.MoveFirst()
						Do Until .Fields("UserGroup").Value = UserGroup
							.MoveNext()
							If .EOF Then Exit Do
						Loop
					End With
					Lbl_GroupName.Text = Adodc2.Recordset.Fields("GroupName").Value
				Else
					Lbl_GroupName.Text = ""
				End If
			End If
			ButtonStatus()
		End Sub

		Private Sub cmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancel.Click
			Adodc1.Recordset.CancelUpdate()
			'Adodc1.Recordset.Bookmark = Adodc1.Recordset.LastModified
			With Adodc2.Recordset
				.MoveFirst()
				Do Until .Fields("UserGroup").Value = Adodc1.Recordset.Fields("UserGroup").Value
					.MoveNext()
					If .EOF Then Exit Do
				Loop
			End With
			ISADDING = False
			ISEDITING = False
			ButtonStatus()
		End Sub

		Private Sub cmdSave_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSave.Click
			Dim trung As Byte
			Dim RecUsers As ADODB.Recordset
			trung = False
			RecUsers = DBconn.Execute("Select * from Users where Name='" & txt_Name.Text & "'")
			If ISADDING Then
				If Not RecUsers.EOF Then trung = True
			Else
				If Not RecUsers.EOF Then
					If RecUsers.Fields("usercode").Value <> Lbl_UserCode.Text Then trung = True
				End If
			End If
			If trung Then
				MsgBox("Da co ten nhan vien nay roi. Xem lai")
				Exit Sub
			End If
			If ISADDING Then
				Lbl_UserCode.Text = PadL(Trim(Str(TakeOrderNumber("Staff", True))), 3, "0")
				DBconn.Execute("Insert into Users (usercode,Name) values ('" & Lbl_UserCode.Text & "','" & txt_Name.Text & "' )")
			Else
				DBconn.Execute("Update Users set Name='" & txt_Name.Text & "' where usercode='" & Lbl_UserCode.Text & "'")
			End If

			'Adodc1.Recordset("Name") = txt_Name
			'Lbl_usercode = Adodc1.Recordset("usercode")
			'Adodc1.Recordset.Update
			Adodc1.RecordSource = "Users"
			Adodc1.Refresh()
			CboName.ReFill()
			'CboName.CtlRefresh()
			CboName.Refresh()
			'Adodc1.Recordset.Bookmark = Adodc1.Recordset.LastModified
			ISADDING = False
			ISEDITING = False
			CboName.BoundText = Lbl_UserCode.Text
			CboName.CtlText = txt_Name.Text
			ButtonStatus()
		End Sub
		Sub ButtonStatus()
			Frame1.Enabled = ISEDITING
			cmdAdd.Enabled = Not ISEDITING
			cmdSave.Enabled = ISEDITING
			cmdCancel.Enabled = ISEDITING
			'cmdExit.Enabled = Not ISEDITING
			cmdAdduser.Enabled = Not ISEDITING And NhomLogin = "1"
			'txt_Name.Visible = ISADDING Or ISEDITING
			CboName.Visible = Not ISADDING
		End Sub

		Private Sub cmdAdd_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAdd.Click
			ISADDING = True
			ISEDITING = True
			ButtonStatus()
			mhnvien()

			'Adodc1.Recordset.AddNew
		End Sub

		Private Sub cmdExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExit.Click
			Me.Close()
		End Sub

		Private Sub cmdAdduser_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAdduser.Click
			Dim myform As New frmAddUser
			myform.Show()
			'frmAddUser/.ShowDialog()
		End Sub

		Private Sub frmStaff_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load

			ISADDING = False
			ISEDITING = False
			MoFile()
			ButtonStatus()
			VB6_AddADODataBinding()
			CboName.RowSource = ADOBind_Adodc1.DataSource
		End Sub
		Sub MoFile()
			'BIENCHUNG
			'MoDataBase
			Adodc1.ConnectionString = ConnectString
			Adodc1.ConnectionTimeout = 40
			Adodc1.CommandType = ADODB.CommandTypeEnum.adCmdTable
			Adodc1.RecordSource = "Users"
			Adodc1.Refresh()

			Adodc1.Recordset.MoveFirst()
			With Adodc1.Recordset
				CboName.CtlText = .Fields("Name").Value
				txt_Name.Text = .Fields("Name").Value
				Lbl_UserCode.Text = .Fields("usercode").Value
				Lbl_UserName.Text = IIf(IsDBNull(.Fields("UserName").Value), "", .Fields("UserName").Value)
			End With
			Adodc2.ConnectionString = ConnectString
			Adodc2.ConnectionTimeout = 40
			Adodc2.CommandType = ADODB.CommandTypeEnum.adCmdTable
			Adodc2.RecordSource = "UserGroup"
			Adodc2.Refresh()

			With Adodc2.Recordset
				.MoveFirst()
				Do Until .Fields("UserGroup").Value = Adodc1.Recordset.Fields("UserGroup").Value
					.MoveNext()
					If .EOF Then Exit Do
				Loop
			End With
			If Not Adodc2.Recordset.EOF Then Lbl_GroupName.Text = Adodc2.Recordset.Fields("GroupName").Value

		End Sub

		Private Sub txt_Name_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txt_Name.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			AnPhim = True
			If KeyAscii = 13 Then

				cmdSave.Focus()
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub
	End Class
End Namespace